package com.ff.discounts.component.productdetail

import android.content.Context
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.ff.discounts.R
import com.ff.discounts.activity.BaseViewModelActivity
import com.ff.discounts.component.tab.home.HistoryData
import com.ff.discounts.databinding.ActivityProductDetailBinding
import com.ff.discounts.superui.decoration.GridDividerItemDecoration
import com.ff.discounts.superui.util.DensityUtil
import com.ff.discounts.superui.util.SuperDarkUtil
import com.ff.discounts.util.ResourceUtil
import com.qmuiteam.qmui.util.QMUIStatusBarHelper


class ProductDetailActivity : BaseViewModelActivity<ActivityProductDetailBinding>() {
    private lateinit var adapter: RecommendAdapter
    private var colorPrimary = 0
    private var textColor = 0
    private var colorBackground = 0
    override fun initDatum() {
        super.initDatum()
        //  // 热门推荐适配器
        adapter = RecommendAdapter()
        binding.recommendList.adapter = adapter
        adapter.submitList(ProductDetailActivity.historyDataList)
    }
    override fun initViews() {
        super.initViews()
        // 设置沉浸式状态栏
        QMUIStatusBarHelper.translucent(this)
        colorPrimary = ResourceUtil.getColorAttributes(
            hostActivity,
            com.google.android.material.R.attr.colorPrimary
        )
        textColor = ResourceUtil.getColorAttributes(
            hostActivity,
            com.google.android.material.R.attr.colorOnSurface
        )
        colorBackground =
            ResourceUtil.getColorAttributes(hostActivity, android.R.attr.colorBackground)

        val itemDecoration = GridDividerItemDecoration(
            binding.recommendList.context,
            DensityUtil.dip2px(binding.recommendList.context, 5f).toInt()
        )
        // 热门推荐
        binding.recommendList.apply {
            layoutManager = GridLayoutManager(hostActivity,2)
            addItemDecoration(itemDecoration)
        }


        //创建通用指示器
       binding.indicator.removeAllTabs()
        for(title in indicatorTitles){
            binding.indicator.addTab(binding.indicator.newTab().setText(title))
        }

    }

    override fun initListeners() {
        super.initListeners()


        binding.back.setOnClickListener {
            finish()
        }

        binding.buyNow.setOnClickListener {
            showBuyDialog()
        }

        //监听列表滚动，目的是显示标题栏
        binding!!.scrollContent.setOnScrollChangeListener(View.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
//            Timber.d("onScrollChange %d %d", scrollY, oldScrollY)
            var alpha = scrollY
            if (alpha > 255) {
                alpha = 255
            }

            //创建一个白色，默认是完全透明，向上滚动，慢慢变为完全不透明
            val toolbarBackgroundColor: Int
            if (SuperDarkUtil.isDark(hostActivity)) {
                //深色模式；完全不透明后就是黑色
                toolbarBackgroundColor = Color.argb(alpha, 0, 0, 0)
            } else {
                //创建一个白色，默认是完全透明，向上滚动，慢慢变为完全不透明
                toolbarBackgroundColor = Color.argb(alpha, 255, 255, 255)
            }
            binding!!.toolbarContainer.setBackgroundColor(toolbarBackgroundColor)
            binding!!.indicator.alpha = (alpha / 255.0).toFloat()
        })

    }

    /**
    * 显示购买dialog
    */
    private fun showBuyDialog() {
        BuyDialogFragment.show(supportFragmentManager){

        }
    }
    companion object{

        private val historyDataList = mutableListOf<HistoryData>(
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集")

        )
        /**
         * 指示器标题
         */
        private val indicatorTitles = intArrayOf(
            R.string.product,
            R.string.evaluation,
            R.string.detail,
            R.string.recommend
        )
    }

}